package com.developer.platform.controller.system;

import com.developer.platform.application.command.DictCmdManager;
import com.developer.platform.application.command.cmd.DictDataAdd;
import com.developer.platform.application.command.cmd.DictDataUpdate;
import com.developer.platform.application.command.cmd.DictTypeAdd;
import com.developer.platform.application.command.cmd.DictTypeUpdate;
import com.developer.platform.application.query.DictQueryManager;
import com.developer.platform.application.query.dto.DictTypeListDto;
import com.developer.platform.application.query.dto.ResponseModel;
import com.developer.platform.controller.BaseController;
import com.developer.platform.infrastructure.enums.StatusEnum;
import com.veeker.core.bean.Pageable;
import com.veeker.log.annotations.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import javax.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ：qiaoliang
 * @date ：2020-11-05
 */
@Api(tags = "字典管理")
@Validated
@RequestMapping("/system/dict")
@RestController
public class DictController extends BaseController {

    private final DictQueryManager dictQueryManager;
    private final DictCmdManager dictCommandManager;

    public DictController(DictQueryManager dictQueryManager,
                          DictCmdManager dictCommandManager) {
        this.dictQueryManager = dictQueryManager;
        this.dictCommandManager = dictCommandManager;
    }

    @Log(businessType = "字典管理",discription = "查询全局字典")
    @ApiOperation(value = "查询全局字典")
    @GetMapping("/overal")
    public ResponseEntity<?> overal(){
        return ResponseEntity.ok(dictQueryManager.overallDict());
    }

    @ApiImplicitParams({
            @ApiImplicitParam(value = "账号", name = "username", required = false),
            @ApiImplicitParam(value = "名称", name = "name", required = false),
            @ApiImplicitParam(value = "手机号", name = "phone", required = false),
            @ApiImplicitParam(value = "页大小", name = "pageSize", required = true),
            @ApiImplicitParam(value = "当前页", name = "pageNumber", required = true),
    })
    @Log(businessType = "字典管理",discription = "查询字典类型列表")
    @ApiOperation(value = "查询字典类型列表")
    @GetMapping
    public ResponseEntity<ResponseModel<DictTypeListDto>> page(@RequestParam(value = "name" , required = false) String name,
                                                               @RequestParam(value = "type" , required = false) String type ,
                                                               @RequestParam(value = "status" , required = false) StatusEnum status ,
                                                               Pageable pageable){
        return ok(dictQueryManager.page(name, type, status, pageable));
    }

    @Log(businessType = "字典管理",discription = "查询字典数据集合")
    @ApiOperation(value = "查询字典数据集合")
    @GetMapping("/data/{type}")
    public ResponseEntity<?> list(@PathVariable(value = "type") String type){
        return ResponseEntity.ok(dictQueryManager.dataList(type));
    }


    @Log(businessType = "字典管理",discription = "新增字典类型")
    @ApiOperation(value = "新增字典类型")
    @PostMapping
    public ResponseEntity<?> save(@Valid @RequestBody DictTypeAdd dictTypeAdd){
        dictCommandManager.saveType(dictTypeAdd);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @Log(businessType = "字典管理",discription = "新增字典数据")
    @ApiOperation(value = "新增字典数据")
    @PostMapping("/data")
    public ResponseEntity<?> save(@Valid @RequestBody DictDataAdd dictDataAdd){
        dictCommandManager.saveData(dictDataAdd);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    @Log(businessType = "字典管理",discription = "修改字典类型")
    @ApiOperation(value = "修改字典类型")
    @PutMapping("/{id}")
    public ResponseEntity<?> update(@PathVariable("id") long id, @Valid @RequestBody DictTypeUpdate dictTypeUpdate){
        dictCommandManager.updateType(id,dictTypeUpdate);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @Log(businessType = "字典管理",discription = "修改字典数据")
    @ApiOperation(value = "修改字典数据")
    @PutMapping("/data/{id}")
    public ResponseEntity<?> update(@PathVariable("id") long id, @Valid @RequestBody DictDataUpdate dictDataUpdate){
        dictCommandManager.updateData(id,dictDataUpdate);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }



    @Log(businessType = "字典管理",discription = "删除字典类型")
    @ApiOperation(value = "删除字典类型")
    @DeleteMapping("/{type}")
    public ResponseEntity<?> delete(@PathVariable("type") String type){
        dictCommandManager.deleteType(type);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    @Log(businessType = "字典管理",discription = "删除字典数据")
    @ApiOperation(value = "删除字典数据")
    @DeleteMapping("/data/{id}")
    public ResponseEntity<?> delete(@PathVariable("id") long id){
        dictCommandManager.deleteData(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


}
